RandomSubset Generic Method

Wintellect PowerCollections

Collapse imageExpand ImageCollapseAll imageExpandAll imageDropDown imageDropDownHover imageCopy imageCopyHover image
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Picks a random subset of count items from collection, and places those items into a random order. No item is selected more than once.

Namespace: Wintellect.PowerCollections
Assembly:  PowerCollections (in PowerCollections.dll)

Syntax

C#
public static T[] RandomSubset<T>(
	IEnumerable<T> collection,
	int count,
	Random randomGenerator
)
Visual Basic (Declaration)
Public Shared Function RandomSubset(Of T) ( _
	collection As IEnumerable(Of T), _
	count As Integer, _
	randomGenerator As Random _
) As T()
Visual C++
public:
generic<typename T>
static array<T>^ RandomSubset (
	IEnumerable<T>^ collection, 
	int count, 
	Random^ randomGenerator
)

Parameters

collection
IEnumerable<(Of <T>)>
The collection of items to select from. This collection is not changed.
count
Int32
The number of items in the subset to choose.
randomGenerator
Random
The random number generates used to make the selection.

Return Value

An array of count items, selected at random from collection.

Type Parameters

T
The type of items in the collection.

Remarks

If the collection implements IList<T>, then this method takes time O(count). Otherwise, this method takes time O(N), where N is the number of items in the collection.

Exceptions

ExceptionCondition
System..::ArgumentOutOfRangeExceptioncount is negative or greater than collection.Count.
System..::ArgumentNullExceptionrandomGenerator is null.

See Also